How orderBy multiple fields in Angular?
How orderBy multiple fields in Angular?
122
17-Jun-2024
Updated on 19-Jun-2024
Ashutosh Kumar Verma
19-Jun-2024Order By Multiple Fileds
In AngularJS, you can use the
orderBy
filter to sort objects based on different fields. Here's how you canorderBy
multiple fields:Syntax
The orderBy filter accepts a set of predicate functions or strings, where each predicate can refer to a field and optionally specifies a sort direction (
+
for ascending,-
for descending).In the example above
items
are the array of objects you want to display.orderBy:['field1', 'field2']
Specifies that the item order should be sorted first in ascending order by field1, then in ascending order by field2 by default.Array data
Let's take an array of objects in the controller,
And you want to show these factors first in ascending order by
city
, then in descending order byage
. You can use orderByIn the syntax above-
orderBy:['city', '-age']
First sort theitems
in ascending order by city ('city
'), then in descending order by age ('-age
').the
city
is arranged in order of ascending order.-age
sorts age in descending order (using-
before the field name in descending order).Example-
Here's a basic example that demonstrates the multiple orderBy properties in Angular js
Output-
Also, Read: How to use a keypress event in AngularJS?